$(function () {
    findById();
});

function findById() {
    $.ajax({
        url: '/back/user/findById',
        data: {id: sessionStorage.getItem("userId")},
        dataType: 'json',
        type: 'get',
        success: function (res) {
            console.log(res)
            setData(res.data);
        }
    })
}

function setData(data) {
    //当我们查询了很多网站时，会提示要刷新表单数据
    layui.use(['form'],
        function () {
            var form = layui.form;
            $("#username").val(data.username);
            $("#birthday").attr('value', data.birthday);
            $("#sex").val(data.sex);
            // $("#enable").val(data.enable);
            $("input[type=radio][name=enable][value=" + data.enable + "]").attr("checked", true);
            // $("#hobby").val(data.hobby);
            var hobbys = data.hobby.split(",");
            $('input[type=checkbox][name=hobby]').each(function () {
                // hobbys.push($(this).attr('title'));
                for (var i = 0; i < hobbys.length; i++) {
                    if ($(this).attr('title') == hobbys[i]) {
                        $(this).attr("checked", true);
                    }
                }
            });
            //注意：一定要刷新
            form.render();
        });
}

layui.use(['laydate', 'form', 'layer', 'jquery'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#birthday' //指定元素
            , trigger: 'click'
        });
        //自定义验证规则
        form.verify({
            // /^[a-Z][a-zA-Z0-9_-]{5,15}/
            username: [/^[a-zA-Z0-9_-]{6,16}$/, '请输入6到16位，用户名只能为英文加数字'],
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function (value) {
                if ($('#password').val() != $('#repass').val()) {
                    return '两次密码不一致';
                }
            }
        });

        //监听提交
        form.on('submit(edit)',
            function (data) {
                data = data.field;
                var hobbys = [];
                $('input[type=checkbox]:checked').each(function () {
                    hobbys.push($(this).attr('title'));
                });
                //数组
                // console.log(hobbys);
                // js怎么讲数组转换位字符串
                data.hobby = hobbys.toString();
                if (data.birthday == '') {
                    data.birthday = '2020/1/1'
                }
                console.log(data);
                //发异步，把数据提交给java
                $.ajax({
                    url: '/back/user/edit',
                    data: data,
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        if (data.count == 1) {
                            layer.alert("编译成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        } else if (data.count == -1) {
                            layer.alert("用户名已经存在");
                        } else {
                            layer.alert("编辑失败");
                        }
                    }
                })
                return false;
            });

    });